﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Linq;
using LightWeightORM.ORM;
using LightWeightORM.ORM.MappingAttribute;


namespace LightWeightORM
{
    namespace ORM
    {
        namespace Utility
        {
            /// <summary>
            /// Summary description for EntityUtility
            /// </summary>
            public class EntityUtility
            {
                public EntityUtility()
                {
                }

                public static string ParseSqlParameter(List<string> fields)
                {
                    StringBuilder sbFields = new StringBuilder();

                    for (int i = 0; i < fields.Count; i++)
                    {
                        if (i == fields.Count - 1) //最后一个字段
                            sbFields.Append("@" + fields[i]);
                        else
                            sbFields.Append("@" + fields[i] + ",");
                    }

                    return sbFields.ToString();
                }
                public static string ParseSqlField(List<string> fields)
                {
                    StringBuilder sbFields = new StringBuilder();

                    for (int i = 0; i < fields.Count; i++)
                    {
                        if (i == fields.Count - 1) //最后一个字段
                            sbFields.Append(fields[i]);
                        else
                            sbFields.Append(fields[i] + ",");
                    }

                    return sbFields.ToString();
                }
                public static string ParseSqlFieldWithParamter(List<string> fields)
                {
                    StringBuilder sbFields = new StringBuilder();

                    for (int i = 0; i < fields.Count; i++)
                    {
                        if (i == fields.Count - 1) //最后一个字段
                            sbFields.Append(string.Format("{0}=@{0}", fields[i]));
                        else
                            sbFields.Append(string.Format("{0}=@{0},", fields[i]));
                    }

                    return sbFields.ToString();
                }

                public static List<string> GetFields(Type tEntity)
                {
                    List<string> fields = new List<string>();
                    PropertyInfo[] prosInfo = tEntity.GetProperties();
                    foreach (PropertyInfo proInfo in prosInfo)
                    {
                        object[] attDataField = null;
                        attDataField = proInfo.GetCustomAttributes(typeof(DataFieldAttribute), false);
                        if (attDataField != null)
                        {
                            fields.Add(((DataFieldAttribute)attDataField[0]).FieldName);
                        }
                    }

                    if (fields.Count == 0)
                    {
                        throw new Exception(string.Format("No fields found, Entity:{0}", tEntity.FullName));
                    }

                    return fields;
                }
                public static string GetTableName(Type tEntity)
                {
                    Attribute[] attrs = Attribute.GetCustomAttributes(tEntity);  // reflection

                    var qAtt = attrs.Where(att => att is TableMappingAttribute);
                    if(qAtt.Count() > 0)
                        return (qAtt.First() as TableMappingAttribute).TableName;
                    else
                        throw new Exception(string.Format("Table name not found, Entity:{0}", tEntity.FullName));

                }
                public static object GetFieldValue(object entity, string fieldName)
                {
                    object value = null;
                    Type tEntity = entity.GetType();

                    PropertyInfo[] prosInfo = tEntity.GetProperties();
                    foreach (PropertyInfo proInfo in prosInfo)
                    {
                        string curfieldName = string.Empty;

                        object[] attDataField = null;
                        attDataField = proInfo.GetCustomAttributes(typeof(DataFieldAttribute), false);
                        if (attDataField != null)
                        {
                            curfieldName = ((DataFieldAttribute)attDataField[0]).FieldName;
                        }

                        if (curfieldName == fieldName)
                        {
                            value = proInfo.GetValue(entity, null);
                        }
                    }

                    return value;
                }
                public static void SetFieldValue(object entity, string fieldName, object value)
                {
                    if (value is DBNull || value == null)
                        return;
                    Type tEntity = entity.GetType();

                    PropertyInfo[] prosInfo = tEntity.GetProperties();
                    foreach (PropertyInfo proInfo in prosInfo)
                    {
                        string curfieldName = string.Empty;

                        object[] attDataField = null;
                        attDataField = proInfo.GetCustomAttributes(typeof(DataFieldAttribute), false);
                        if (attDataField != null)
                        {
                            curfieldName = ((DataFieldAttribute)attDataField[0]).FieldName;
                        }

                        if (curfieldName == fieldName)
                        {
                            proInfo.SetValue(entity, value, null);
                            break;
                        }
                    }
                }
                public static string GetPrimaryKey(Type tEntity)
                {
                    Attribute[] attrs = Attribute.GetCustomAttributes(tEntity);  // reflection

                    var qAtt = attrs.Where(att => att is TableMappingAttribute);
                    if (qAtt.Count() > 0)
                        return (qAtt.First() as TableMappingAttribute).PrimaryKey;
                    else
                        return string.Empty;
                }

                public static object GetPrimaryKeyValue(object entity)
                {
                    return GetFieldValue(entity, GetPrimaryKey(entity.GetType()));
                }
            }
        }
    }
}